package com.zhaohang.cn.mybasicactivity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.zhaohang.cn.broad.BoardCastUtils;
import com.zhaohang.cn.broad.PrensentView;
import com.zhaohang.cn.utils.AppManager;
import com.zhaohang.cn.utils.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/10/22 20:49
 * 描述:
 */
public class BasicAllActivity extends AppCompatActivity implements PrensentView {

    private              String[]     permissionss     = new String[]{
            Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
    };
    private              List<String> mPermissionList;
    private static final int          MY_SINGLE        = 100;
    private static final int          MY_PERMISIONLIST = 101;
    private BoardCastUtils mCastUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //添加到栈中,将所有的Activity到集合当中
        AppManager.getInstance().addActivity(this);
        //获取6.0权限
        getListPermissions(permissionss);


        //设置网络监听
        //注册监听网络等这里还需要再加一个String flg来确定请求框架，暂时只是留个接口

        if (needNetListion()) {
            mCastUtils = new BoardCastUtils();
            mCastUtils.netWork(this);
            mCastUtils.sreamWork(this);
        }

    }

    /**
     * 判断是否有网络
     */
    protected boolean isConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
    }

    protected void getListPermissions(String[] permissionss) {
        // 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
        if (mPermissionList == null) {
            mPermissionList = new ArrayList<>();
        } else {
            mPermissionList.clear();
        }

        //检查版本是否大于M
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            for (int i = 0; i < permissionss.length; i++) {
                if (ContextCompat.checkSelfPermission(this, permissionss[i]) != PackageManager.PERMISSION_GRANTED) {
                    mPermissionList.add(permissionss[i]);
                }
            }
            if (!mPermissionList.isEmpty()) {
                //将List转为数组
                String[] permissionarrs = mPermissionList.toArray(new String[mPermissionList.size()]);
                ActivityCompat.requestPermissions(BasicAllActivity.this, permissionarrs, MY_PERMISIONLIST);
            }
        }
    }

    /**
     * @param permision 需要的单个权限
     */
    protected boolean getSinglePermission(String permision) {
        //检查版本是否大于M
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, permision) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{permision},
                        MY_SINGLE);
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        //单项回调
        if (requestCode == MY_SINGLE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //                    showToast("权限已申请");
            } else {
                //                    showToast("权限已拒绝");
                //判断是否勾选禁止后不再询问
                boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(BasicAllActivity.this, permissions[0]);
                if (!showRequestPermission) {
                    Intent intent = new Intent(
                            Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
            }
        } else if (requestCode == MY_PERMISIONLIST) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                    //判断是否勾选禁止后不再询问
                    boolean showRequestPermission = ActivityCompat.shouldShowRequestPermissionRationale(BasicAllActivity.this, permissions[i]);
                    if (showRequestPermission) {
                        //                        Intent intent = new Intent(
                        //                                Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        //                        intent.setData(Uri.parse("package:" + getPackageName()));
                        //                        startActivity(intent);

                        Util.goToSetting();
                    }
                }
            }
        }
    }


    /**
     * @param clazz   要跳转到的类
     * @param isFinsh 是否销毁当前界面
     */
    protected void skipOtherActivity(Class clazz, boolean isFinsh) {
        startActivity(new Intent(BasicAllActivity.this, clazz));
        if (isFinsh) {
            BasicAllActivity.this.finish();
        }
    }


    /**
     *
     * @return 是否开启网络监听
     */
    protected boolean needNetListion() {


        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (needNetListion()) {
            mCastUtils.unRegistNetWork(BasicAllActivity.this);
            mCastUtils.unRegistScreamWork(BasicAllActivity.this);
        }

        AppManager.getInstance().finishActivity(this); //从栈中移除
    }


    @Override
    public void onNetChange(int netMobile) {

    }

    @Override
    public void onScreamState(int flg) {

    }
}
